package com.mmy.controller.plane;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.mmy.controller.Common;
import com.mmy.domain.constzone.TConstZone;
import com.mmy.domain.plane.TPlaneBase;
import com.mmy.domain.plane.TPlaneOrder;
import com.mmy.domain.plane.TPlaneOrg;
import com.mmy.domain.plane.TPlanePlan;
import com.mmy.domain.plane.VPlaneAirport;
import com.mmy.domain.plane.VPlanePlan;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.impl.constzone.ConstZoneServiceImpl;
import com.mmy.service.plane.impl.PlaneAirportServiceImpl;
import com.mmy.service.plane.impl.PlaneBaseServiceImpl;
import com.mmy.service.plane.impl.PlaneOrderServiceImpl;
import com.mmy.service.plane.impl.PlaneOrgServiceImpl;
import com.mmy.service.plane.impl.PlanePlanServiceImpl;
import com.mmy.service.plane.impl.TPlanePlanServiceImpl;

/**
 * 类的介绍：航班时刻
 *
 * @author shangzl
 * @date 创建日期：
 * @version 1.0
 */

@Controller
public class PlanePlanController {
	@Autowired
	PlaneBaseServiceImpl planeBaseServiceImpl;
	@Autowired
	PlaneOrgServiceImpl planeOrgServiceImpl;
	@Autowired
	PlanePlanServiceImpl planePlanServiceImpl;
	@Autowired
	PlaneOrderServiceImpl planeOrderServiceImpl;
	@Autowired
	Common common;
	@Autowired
	ConstZoneServiceImpl constZoneServiceImpl;
	@Autowired
	PlaneAirportServiceImpl planeAirportServiceImpl;
	@Autowired
	TPlanePlanServiceImpl tPlanePlanServiceImpl;

	/* 前台 */
	/**
	 * 函数功能说明： 跳转进入前台飞机机票页面
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人Hanxl
	 * @date 创建日期2021.02.24 修改时间2021.03.17
	 */

	@RequestMapping(value = "/fronttoplaneindex")
	public String toFrontPlaneIndex(Model model) {
		// 国内机票推荐模块 推荐由北京出发的机票信息 前10条
		String strZoneOutName = "北京市";
		List<VPlanePlan> lstPlanePlan = planePlanServiceImpl.findByZoneOutName(strZoneOutName);
		model.addAttribute("lstPlanePlan", lstPlanePlan);
		// 国外机票推荐模块 推荐由北京出发的机票信息 前10条
		List<VPlanePlan> lstOutPlanePlan = planePlanServiceImpl.findOutByZoneOutName(strZoneOutName);
		model.addAttribute("lstOutPlanePlan", lstOutPlanePlan);
		// 显示所有的航空公司
		List<TPlaneOrg> lstPlaneOrg = planeOrgServiceImpl.findAll();
		model.addAttribute("lstPlaneOrg", lstPlaneOrg);
		return "plane/PlaneIndex";

	}

	/**
	 * 函数功能说明： 跳转 后台进入航班时刻
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
	@RequestMapping(value = "/backtoplaneplanindex")
	public String backPlanePlanIndex(HttpServletRequest request, HttpSession session, Model model) {
		// 创建的航班信息有所属旅行社 如果为超级管理员显示全部 为其他有权限的只能查看本旅行社的航班
		// 下拉框测试
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		String strPlaneID = request.getParameter("iPlaneID");
		List<VPlanePlan> lstPlanePlan = planePlanServiceImpl.findPlanePlan(sessionSysUser, strPlaneID);
		model.addAttribute("lstPlanePlan", lstPlanePlan);
		model.addAttribute("strPlaneID", strPlaneID);
		return "webbackground/plane/PlanePlanIndex";
	}

	/**
	 * 函数功能说明： 异步--航班信息列表数据
	 * 
	 * @param 参数说明 vPlaneBase
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.03.17
	 *
	 */
	@RequestMapping("/backplaneplanjson")
	@ResponseBody
	public String backPlanePlanJson(HttpServletRequest request, HttpSession session, Model model) {

		// 接收的分页信息处理
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		int startIndex = (page - 1) * rows;
		int count = rows;
		// 搜索条件
		String strPlaneOutDate = request.getParameter("planeOutDate");
		String strPlaneOutZone = request.getParameter("planeOutZone");
		String strPlaneInZone = request.getParameter("planeInZone");
		String strPlaneID = request.getParameter("iPlaneID");

		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");

		// 表格数据
		List<VPlanePlan> lstPlanePlan = planePlanServiceImpl.findByOrgID(strPlaneID, sessionSysUser, strPlaneOutDate,
				strPlaneOutZone, strPlaneInZone, startIndex, count);

		model.addAttribute("lstPlanePlan", lstPlanePlan);
		// 数据总数量
		int total = planePlanServiceImpl.queryPlanCount(strPlaneID, sessionSysUser, strPlaneOutDate, strPlaneOutZone,
				strPlaneInZone);
		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("rows", lstPlanePlan);
		ret.put("total", total);
		return JSONObject.toJSONString(ret);
	}

	@RequestMapping(value = "/planeplaninfo")
	public String ToPlanePlanInfo(HttpServletRequest request, Model model) {

		int iPlanID = Integer.parseInt(request.getParameter("iPlanID"));

		VPlanePlan vPlanePlan = tPlanePlanServiceImpl.findByID(iPlanID);
		model.addAttribute("vPlanePlan", vPlanePlan);

		return "webbackground/plane/planeplaninfo";
	}

	/**
	 * 函数功能说明：后台 进入新建航班时刻信息页面
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人hanxl
	 * @date 创建日期 修改时间 2021.3.17
	 */
	@RequestMapping(value = "/backcreateplaneplan")
	public String backCreatePlanePlan(HttpServletRequest request, Model model) {
		String strFlag = request.getParameter("flag");
		String strPlaneID = request.getParameter("iPlaneID");
		TPlaneBase dbPlaneBase = planeBaseServiceImpl.findByPlaneID(Integer.parseInt(strPlaneID));
		model.addAttribute("dbPlaneBase", dbPlaneBase);
		if (Integer.parseInt(strFlag) == 0) {
			// 查询省
			List<TConstZone> lstZoneProvince = constZoneServiceImpl.findAllProvince();
			model.addAttribute("lstZoneProvince", lstZoneProvince);

		} else {

			// 查询所有的洲
			List<TConstZone> lstZoneContinent = constZoneServiceImpl.findAllContinent();
			model.addAttribute("lstZoneContinent", lstZoneContinent);
		}

		model.addAttribute("strFlag", strFlag);
		return "webbackground/plane/CreatePlanePlan";
	}

	/**
	 * 函数功能说明： 新建航班时刻
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
	@RequestMapping(value = "/backsavecreateplaneplan")
	public String backSaveCreatePlanePlan(TPlanePlan tPlanePlan, HttpServletRequest request, String planStartDate,
			String planEndDate, String planWeekNum, String planStartTime, HttpSession session) {
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		planePlanServiceImpl.createPlanePlan(tPlanePlan, planStartDate, planEndDate, planWeekNum, planStartTime,
				sessionSysUser);
		return "redirect:backtoplaneplanindex?iPlaneID=" + tPlanePlan.getPlaneID();
	}

	/**
	 * 函数功能说明： 删除航班时刻
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@PostMapping("/delplaneplan")
	@ResponseBody
	public String DelPlanePlan(String checkList) {

		String[] checklist = checkList.split(",");
		List<Integer> lstPlanID = new ArrayList<>();

		for (String str : checklist) {
			lstPlanID.add(Integer.parseInt(str));
		}

		// tPlanePlanServiceImpl.delPlanePlan(lstPlanID);

		return "webbackground/plane/planeplan";
	}

	/**
	 * 函数功能说明： 修改航班时刻
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/backtoupdateplaneplan")
	public String backToUpdatePlanePlan(HttpServletRequest request, Model model) {

		int iPlanID = Integer.parseInt(request.getParameter("iPlanID"));

		VPlanePlan dbPlanePlan = planePlanServiceImpl.findByPlanID(iPlanID);
		model.addAttribute("dbPlanePlan", dbPlanePlan);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String strStartDate = sdf.format(dbPlanePlan.getStartDate());
		String strEndDate = sdf.format(dbPlanePlan.getEndDate());
		model.addAttribute("strStartDate", strStartDate);
		model.addAttribute("strEndDate", strEndDate);
		if (dbPlanePlan.getZoneOutID() < 900000) {
			// 查询所有省
			List<TConstZone> lstZoneProvince = constZoneServiceImpl.findAllProvince();
			model.addAttribute("lstZoneProvince", lstZoneProvince);
			// 获取出发地所在的省
			int strProvinceOutID = constZoneServiceImpl.findProvince(dbPlanePlan.getZoneOutID()).getZoneID();
			model.addAttribute("strProvinceOutID", strProvinceOutID);
			// 查询出发地所在省 所有的市
			List<TConstZone> lstZoneCityOut = constZoneServiceImpl.findCities(dbPlanePlan.getZoneOutID());
			model.addAttribute("lstZoneCityOut", lstZoneCityOut);
			// 获取出发地所在的省
			int strProvinceInID = constZoneServiceImpl.findProvince(dbPlanePlan.getZoneInID()).getZoneID();
			model.addAttribute("strProvinceInID", strProvinceInID);
			// 查询出发地所在省 所有的市
			List<TConstZone> lstZoneCityIn = constZoneServiceImpl.findCities(dbPlanePlan.getZoneOutID());
			model.addAttribute("lstZoneCityIn", lstZoneCityIn);

		} else {
			// 查询所有的洲
			List<TConstZone> lstZoneContinent = constZoneServiceImpl.findAllContinent();
			model.addAttribute("lstZoneContinent", lstZoneContinent);
			// 获取出发地所在的洲
			int strContinentOutID = constZoneServiceImpl.findContinent(dbPlanePlan.getZoneOutID()).getZoneID();
			model.addAttribute("strContinentOutID", strContinentOutID);
			// 查询出发地所在洲 所有的国家
			List<TConstZone> lstZoneCountryOut = constZoneServiceImpl.findCountriess(dbPlanePlan.getZoneOutID());
			model.addAttribute("lstZoneCountryOut", lstZoneCountryOut);
			// 获取出发地所在的洲
			int strContinentInID = constZoneServiceImpl.findContinent(dbPlanePlan.getZoneInID()).getZoneID();
			model.addAttribute("strContinentInID", strContinentInID);
			// 查询出发地所在洲 所有的国家
			List<TConstZone> lstZoneCountryIn = constZoneServiceImpl.findCountriess(dbPlanePlan.getZoneOutID());
			model.addAttribute("lstZoneCountryIn", lstZoneCountryIn);
		}

		List<VPlaneAirport> lstPlaneAirport = planeAirportServiceImpl.findByZoneID(dbPlanePlan.getZoneOutID());
		model.addAttribute("lstPlaneAirport", lstPlaneAirport);
		List<VPlaneAirport> lstPlaneAirportIn = planeAirportServiceImpl.findByZoneID(dbPlanePlan.getZoneInID());
		model.addAttribute("lstPlaneAirportIn", lstPlaneAirportIn);

		return "webbackground/plane/UpdatePlanePlan";

	}

	/**
	 * 函数功能说明： 修改航班时刻
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/backsaveupdateplaneplan")
	public String backSaveUpdatePlanePlan(TPlanePlan tPlanePlan, String planStartDate, String planEndDate,
			HttpSession session) {
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		planePlanServiceImpl.updatePlanePlan(tPlanePlan, planStartDate, planEndDate, sessionSysUser);
		return "redirect:backtoplaneplanindex?iPlaneID=" + tPlanePlan.getPlaneID();
	}

	/**
	 * 函数功能说明： 查询航班时刻
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人hanxl
	 * @date 创建日期 修改时间 2021.03.17
	 */
	@RequestMapping(value = "/frontsearchplaneplan")
	public String backSearchPlanePlan(HttpServletRequest request, Model model) {

		int iIsReturn = Integer.parseInt(request.getParameter("strIsReturn"));
		List<VPlanePlan> lstPlanePlan = new ArrayList<VPlanePlan>();
		if (iIsReturn == 0) {
			String strOutCityName = request.getParameter("homecity_name1");
			String strcityid = request.getParameter("cityid");
			String strInCityName = request.getParameter("getcity_name1");

			String strgetcityid = request.getParameter("getcityid");
			// 前台推荐机票获取的时间精确到秒
			String strOutDate = request.getParameter("strOutDate");
			lstPlanePlan = planePlanServiceImpl.findSingle(strOutCityName, strInCityName, strOutDate);
			model.addAttribute("strOutCityName", strOutCityName);
			model.addAttribute("strcityid", strcityid);
			model.addAttribute("strInCityName", strInCityName);
			model.addAttribute("strgetcityid", strgetcityid);

			model.addAttribute("strOutDate", strOutDate);
		} else {
			String strOutCityName = request.getParameter("homecity_name2");
			String strcityid = request.getParameter("cityid");
			String strInCityName = request.getParameter("getcity_name2");
			String strgetcityid = request.getParameter("getcityid");
			String strOutDate = request.getParameter("strOutDate1");
			String strBackDate = request.getParameter("strBackDate1");
			// 去程
			lstPlanePlan = planePlanServiceImpl.findSingle(strOutCityName, strInCityName, strOutDate);
			// 返程
			List<VPlanePlan> lstBackPlanePlan = planePlanServiceImpl.findSingle(strInCityName, strOutCityName,
					strBackDate);
			model.addAttribute("strOutCityName2", strOutCityName);
			model.addAttribute("strcityid2", strcityid);
			model.addAttribute("strInCityName2", strInCityName);
			model.addAttribute("strgetcityid2", strgetcityid);
			model.addAttribute("strOutDate1", strOutDate);
			model.addAttribute("strBackDate1", strBackDate);
			model.addAttribute("lstBackPlanePlan", lstBackPlanePlan);

		}
		model.addAttribute("strIsReturn", iIsReturn);
		model.addAttribute("lstPlanePlan", lstPlanePlan);
		// 显示所有的航空公司
		List<TPlaneOrg> lstPlaneOrg = planeOrgServiceImpl.findAll();
		model.addAttribute("lstPlaneOrg", lstPlaneOrg);
		return "plane/PlaneSearch";

	}

	/**
	 * 函数功能说明：前台 新建飞机票预留信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人hanxl
	 * @date 创建日期 修改时间 2021.3.17
	 */
	@RequestMapping(value = "/enterreserveplane")
	public String EnterReservePlane(HttpServletRequest request, Model model) {

		// 查询该景区时间在计划日期内的计划

		VPlanePlan dbPlanePlan = planePlanServiceImpl.findByPlanID(Integer.parseInt(request.getParameter("iPlanID")));
		model.addAttribute("dbPlanePlan", dbPlanePlan);

		model.addAttribute("iPlanID", request.getParameter("iPlanID"));
		return "plane/ReservePlane";

	}

	/**
	 * 函数功能说明： 新建机票订单信息提交
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.03.17
	 */
	@ResponseBody
	@RequestMapping(value = "/savereserveplane")
	public Map<String, Object> saveReservePlane(@RequestParam("planID") String strPlanID,
			@RequestParam("order") String strPlaneOrder, @RequestParam("number") String strNumber,
			HttpSession session) {

		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		Map<String, Object> data = planeOrderServiceImpl.createOrderPlane(strPlanID, strNumber, strPlaneOrder,
				sessionSysUser);

		return data;
	}

	/**
	 * 函数功能说明： 新建景区电子票订单信息提交 进入填写收据页面
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.01.12
	 */
	@RequestMapping(value = "/topayorderplane")
	public String toPayOrderPlane(HttpServletRequest request, Model model) {
		String lstID = request.getParameter("lstID");
		// 拆分字符串
		String[] arrID = lstID.split(",");
		TPlaneOrder dbPlaneOrder = planeOrderServiceImpl.findByID(Integer.parseInt(arrID[0]));
		model.addAttribute("iOrderAmount", dbPlaneOrder.getOrderAmount());
		model.addAttribute("lstID", lstID);
		return "plane/PayOrderPlane";
	}

	/**
	 * 函数功能说明：收据提交
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @author 创建人 shangzl 修改人hanxl
	 * @date 创建日期 2021.01.12
	 */
	@RequestMapping(value = "/savepayorderplane")
	public String savePayOrderPlane(TPlaneOrder tPlaneOrder, HttpSession session, String lstID, Model model) {
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		List<TPlaneOrder> lstPlaneOrder = planeOrderServiceImpl.updatePlaneOrder(tPlaneOrder, sessionSysUser, lstID);

		model.addAttribute("lstID", lstID);
		model.addAttribute("tPlaneOrder", lstPlaneOrder.get(0));
		model.addAttribute("lstPlaneOrder", lstPlaneOrder);
		return "plane/PrintPlaneOrder";

	}

}
